   // ****************************************************** 
   //
   // Copyright 2011 OpenAjax Alliance
   //
   // Licensed under the Apache License, Version 2.0 (the "License");
   // you may not use this file except in compliance with the License.
   // You may obtain a copy of the License at
   //
   //    http://www.apache.org/licenses/LICENSE-2.0
   //
   // Unless required by applicable law or agreed to in writing, software
   // distributed under the License is distributed on an "AS IS" BASIS,
   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   // See the License for the specific language governing permissions and
   // limitations under the License.
   //
   // ****************************************************** 
   
OAA_CI.images = OAA_CI.images || {}; 
OAA_CI.media = OAA_CI.media || {}; 
OAA_CI.abbreviations = OAA_CI.abbreviations || {}; 

/** ================================================================
*
* Module initialization
*
*
* =============================================================== */

OAA_CI.images.last_index = 0;
OAA_CI.media.last_index = 0;
OAA_CI.abbreviations.last_index = 0;

/** ================================================================
*
* onLoad
*
* @desc Initializes images module
*
* @return nothing
*
* =============================================================== */

OAA_CI.images.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.images_nls_strings = document.getElementById("ID_STRINGBUNDLE_IMAGES");        
  
  OAA_CI.images.onShowImagesCommand();
}; 

/** ================================================================
*
* onLoad
*
* @desc Initializes images module
*
* @return nothing
*
* =============================================================== */

OAA_CI.media.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.media_nls_strings = document.getElementById("ID_STRINGBUNDLE_MEDIA");        
  
}; 


/** ================================================================
*
* onShowImagesCommand
*
* @desc Evaluates the document and populates the XUL tree with image information
*
* @return nothing
*
* =============================================================== */

OAA_CI.images.onShowImagesCommand = function(e) {

  var i;
  var ie;
  var xul_treeitem;

  OAA_CI.evaluateDocument();   
  
  var images_cache = OAA_CI.evaluation.dom_cache.images_cache;  
  images_cache.sortImageElements('document_order', true);
  var image_elements = images_cache.image_elements;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_IMAGES');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  var image_elements_len = image_elements.length;
  
  if (image_elements_len) {
    for (i=0; i<image_elements_len; i++) {
      ie = image_elements[i];
      OAA_CI.addTreeItem(xul_treechildren, [ie.document_order, ie.source, ie.alt], ie.cache_id);
    }  // end loop
  }  
  else {
    xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
    
  OAA_CI.updateXULTime(start_time);
    

};  // end imageListBox Function


/** ================================================================
*
* onImageElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the ImageCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.images.onImageElementItemsSelectChanged = function (event) {

  var ie;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var images_cache = OAA_CI.evaluation.dom_cache.images_cache;  
  var xul_tree = document.getElementById('ID_TREE_IMAGES');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    ie = images_cache.getImageElementByCacheId(cache_id);
    
    if (ie) {
      OAA_CI.showCacheItemProperties(ie);
    }
  } 
    
};

/** ================================================================
*
* onShowMediaCommand
*
* @desc Evaluates the document and populates the XUL tree with media information
*
* @return nothing
*
* =============================================================== */

OAA_CI.media.onShowMediaCommand = function(e) {

  var i;
  var me;

  OAA_CI.evaluateDocument();   
  
  var media_cache = OAA_CI.evaluation.dom_cache.media_cache;  
  media_cache.sortMediaElements('document_order', true);
  var media_elements = media_cache.media_elements;
 
  var start_time = new Date().getTime();
  
  var xul_treechildren = document.getElementById('ID_TREECHILDREN_MEDIA');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  var media_elements_len = media_elements.length;
  
  OpenAjax.a11y.console("Number of media: " + media_elements_len);

  if (media_elements_len) {
    for (i=0; i<media_elements_len; i++) {
      me = media_elements[i];
      OAA_CI.addTreeItem(xul_treechildren, [me.document_order, me.dom_element.tag_name, me.is_video, me.is_audio, me.has_caption, me.is_audio_desc], me.cache_id);
    }  // end loop
  }  
  else {
    var xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","","","","",""], "none");
    xul_treechildren.appendChild(xul_treeitem);
  }
    
  OAA_CI.updateXULTime(start_time);
    

};  // end imageListBox Function


/** ================================================================
*
* onMediaElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the ImageCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.media.onMediaElementItemsSelectChanged = function (event) {

  var me;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var media_cache = OAA_CI.evaluation.dom_cache.media_cache;  
  var xul_tree = document.getElementById('ID_TREE_MEDIA');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Media cache id: " + cache_id );

    me = media_cache.getMediaElementByCacheId(cache_id);
    
    if (me) {
      OAA_CI.showCacheItemProperties(me);
    }
  } 
   
};


/** ================================================================
*
* onLoad
*
* @desc Initializes abbreviation module
*
* @return nothing
*
* =============================================================== */

OAA_CI.abbreviations.onLoad = function(oaa_cache_inspector) {
  
  OAA_CI.abbreviations_nls_strings = document.getElementById("ID_STRINGBUNDLE_ABBREVIATIONS");        
   
}; 

/** ================================================================
*
* onShowAbbreviationsCommand
*
* @desc Evaluates the document and populates the user interface controls
*       with abbreviation information
*
* @return nothing
*
* =============================================================== */

OAA_CI.abbreviations.onShowAbbreviationsCommand = function(e) {

  var i;
  var ai;
  var des;
  var des_len;
  var abbreviations_cache;
  var abbreviation_items;
  var xul_treeitem;
  var xul_treechildren;
  var abbreviation_items_len;
  
  OAA_CI.evaluateDocument();   
  
  abbreviations_cache = OAA_CI.evaluation.dom_cache.abbreviations_cache;  
  
  abbreviations_cache.sortAbbreviationItems(true);  
  
  abbreviation_items = abbreviations_cache.abbreviation_items;
 
  var start_time = new Date().getTime();
  
  xul_treechildren = document.getElementById('ID_TREECHILDREN_ABBREVIATIONS');
  
  OAA_CI.removeChildNodes(xul_treechildren);

  abbreviation_items_len = abbreviation_items.length;
  
  OpenAjax.a11y.console("Abbreviation children object: " + abbreviation_items_len);
  
  xul_treeitem = null;
    
  if (abbreviation_items_len) {
    for (i=0; i < abbreviation_items_len; i++ ) { 

      ai = abbreviation_items[i];

      xul_treeitem = OAA_CI.createXULElement('treeitem', ["container=true","open=false"]);

      OAA_CI.addTreeRow(xul_treeitem, [ai.count, ai.abbreviation_text, "" ], ai.cache_id );

      var xul_sub_treechildren = OAA_CI.createXULElement('treechildren',[]);

      des = ai.dom_elements;
      des_len = des.length;

      for(var j=0; j<des_len; j++) {
        de = des[j];
        OAA_CI.addTreeItem(xul_sub_treechildren, [de.tag_name, ai.abbreviation_text, de.title ], de.cache_id );
      } // loop
    
      xul_treeitem.appendChild(xul_sub_treechildren);
    
      xul_treechildren.appendChild(xul_treeitem);

    } // end loop  
  }
  else {
    xul_treeitem = OAA_CI.createXULElement('treeitem', []);
    OAA_CI.addTreeRow(xul_treeitem, ["none","",""], "none" );
    xul_treechildren.appendChild(xul_treeitem);
  }
  OAA_CI.updateXULTime(start_time);
    
};  


/** ================================================================
*
* onAbbreviationElementItemsSelectChanged
*
* @desc Updates properties tappanel with the information from the currently selected 
*       item in the ColorContrastCache
*
* @return nothing
*
* =============================================================== */

OAA_CI.abbreviations.onAbbreviationsItemSelectChanged = function (event) {

  var de;
  var ai;
  var col_id;
  var cache_id;
  
  var index = event.target.currentIndex;
  var element_cache = OAA_CI.evaluation.dom_cache.element_cache;  
  var abbreviations_cache = OAA_CI.evaluation.dom_cache.abbreviations_cache;  
  var xul_tree = document.getElementById('ID_TREE_ABBREVIATIONS');
  
  if (xul_tree) {
    col_id   = xul_tree.columns.getFirstColumn();
    cache_id = xul_tree.view.getCellValue(index, col_id);

    OpenAjax.a11y.console("Abbreviation cache id: " + cache_id );

    ai = abbreviations_cache.getAbbreviationItemById(cache_id);
    
    if (ai) {
      OAA_CI.showCacheItemProperties(ai);
    }
    else {
      de = element_cache.getDOMElementByCacheId(cache_id);
      OpenAjax.a11y.console("DOM Element: " + de);
      if (de) { 
        OAA_CI.showCacheItemProperties(de);
      }
    }  
  } 

};
